package com.dhwl.hsh.merchant.ui.fragment;

import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.ui.activity.LoginActivity;


/**
 * Guide 的引导页面
 *
 * @author Derek.Kidd
 * @time 2016/4/29 11:02
 */
public final class GuideFragment extends Fragment {

    private int position;

    private TypedArray bgColorArr;
    private TypedArray iconArr;

    private String[] titleStrArr;
    private String[] infoArr;


    public static GuideFragment newInstance(int position) {
        GuideFragment fragment = new GuideFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        position = bundle.getInt("position");
        bgColorArr = getActivity().getResources().obtainTypedArray(R.array.guide_bg_color);
        iconArr = getActivity().getResources().obtainTypedArray(R.array.guide_icon);

        titleStrArr = getActivity().getResources().getStringArray(R.array.guide_title);
        infoArr = getActivity().getResources().getStringArray(R.array.guide_info);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View layout = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_guide, null);
        ImageView icon = (ImageView) layout.findViewById(R.id.iv_guide);
        TextView tvTitle = (TextView) layout.findViewById(R.id.tv_guide_title);
        TextView tvInfo = (TextView) layout.findViewById(R.id.tv_guide_info);
        Button tvNext = (Button) layout.findViewById(R.id.btn_guide_next);

        layout.setBackgroundColor(bgColorArr.getColor(position,0));
        icon.setImageResource(iconArr.getResourceId(position,0));
        tvTitle.setText(titleStrArr[position]);
        tvInfo.setText(infoArr[position]);

        if(position==titleStrArr.length-1){
            tvNext.setVisibility(View.VISIBLE);
            tvNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                    getActivity().finish();
                }
            });
        }else{
            tvNext.setVisibility(View.INVISIBLE);
        }
        return layout;
    }

}
